package cn.edu.zjut.kelvin;

import java.text.DecimalFormat;
import java.util.List;

import org.hibernate.Query;


import cn.edu.zjut.kelvin.sessionfactory.*;
import cn.edu.zjut.kelvin.dao.*;
import cn.edu.zjut.kelvin.reader.Reader;


/**
 *     
 * @author Kelvin
 * @date 2012-5-28
 * #异构数据库之间(Oracle 10/g to MySQL)增量复制
 * 手动改的地方:
 *     - mysql 配置文件my.ini 中 max_allowed_packet 调大，如 100M，事后改回
 *     - mysql 数据库中对应表 RD_ZP 一列类型由BLOB手动调整为LONGBLOB
 */
public class Main {
	static int start = 28500;//起始位置
	static int limit = 300;//每次分片大小
	
	public static void duplicate(){
		java.text.SimpleDateFormat simpleDateFormat = 
			new java.text.SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
		DecimalFormat df = new DecimalFormat("00.00");
		int count = 0;
		Double percent = 0.0;
		
		SrcReaderDAO srcDAO = new SrcReaderDAO();		//source DAO
		DestReaderDAO destDAO = new DestReaderDAO();	//destination DAO

		count = srcDAO.getRowCount();
		//count = limit;//for test
		
		System.out.println("-----------------");
		System.out.println("-Start From Here-");
		System.out.println("-----------------");
		System.out.println(simpleDateFormat.format(new java.util.Date()) + ": " 
				+ "00.00% (0/" + count + ")");
		
		for (; start < count; start += limit){
			List<Reader> tmp = destDAO.findAll(start,0x1);
			if (tmp.size() != 0){
				System.out.println(Reader.class.getSimpleName() 
						+ " Record[" + start + "," + limit + ") catched" );
				//continue;
			}
			List<Reader> list = srcDAO.findAll(start,limit);
			destDAO.save(list);
			percent = start * 100.0 / count;
			System.out.println(simpleDateFormat.format(new java.util.Date()) + "  " 
					+ df.format(percent) + "% ("+ (start + limit) + "/" + count + ")");
		}
		
		System.out.println(simpleDateFormat.format(new java.util.Date()) + ": " 
				+ "99.99% (" + count + "/" + count + ") #All is Well");

	}

	public static void main(String sp[]){
		duplicate();
	}
	
	
}
